package com.borqsprocessmanager.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.R;
import com.borqsprocessmanager.info.ReceiverProcessInfo;
import com.borqsprocessmanager.info.SectionInfo;
import com.borqsprocessmanager.util.Log;
import com.borqsprocessmanager.view.MySwitch;

public class ProcessAdapter extends SectionAdapter implements
        OnCheckedChangeListener, OnClickListener {
    private final String TAG = "ResAdapter";

    private OnCheckedChangeListener mOnCheckedChangeListener;

    private ArrayList<ReceiverProcessInfo> mList;
    private LayoutInflater mInflater;
    private PackageManager pm;

    private boolean isChecked = false;

    public ProcessAdapter(Context context, ArrayList<ReceiverProcessInfo> list) {
        mList = list;
        mInflater = LayoutInflater.from(context);
        pm = context.getPackageManager();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.v(TAG, "position===" + position);

        ViewHolder holder;
        ReceiverProcessInfo info = mList.get(position);
        Log.v(TAG, "info.appName===" + info.appName);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.process_list_item, null);
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView
                    .findViewById(R.id.pro_item_image_icon);
            holder.mSwitch = (MySwitch) convertView
                    .findViewById(R.id.pro_item_switch);
            holder.aotuType = (TextView) convertView
                    .findViewById(R.id.pro_item_text_abnormal);
            holder.normalType = (TextView) convertView
                    .findViewById(R.id.pro_item_text_normal);
            holder.appName = (TextView) convertView
                    .findViewById(R.id.pro_item_text_name);

            holder.mSwitch.setOnClickListener(this);
            holder.mSwitch.setOnCheckedChangeListener(this);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        try {
            holder.icon.setImageDrawable(pm
                    .getApplicationIcon(info.packageName));
        } catch (NameNotFoundException e) {
            holder.icon.setImageDrawable(App.mContext.getResources()
                    .getDrawable(R.drawable.android));
        }
        holder.appName.setText(info.appName);
        Log.v(TAG, "info.appName===" + info.appName);
        holder.mSwitch.setTag(position);
        if (info.isClose == 2) {
            holder.mSwitch.setChecked(false);
        } else {
            holder.mSwitch.setChecked(true);
        }

        if (info.type == ReceiverProcessInfo.AUTOP_ROCESS) {
            holder.aotuType.setVisibility(View.VISIBLE);
            holder.normalType.setVisibility(View.GONE);
        } else if (info.type == ReceiverProcessInfo.COMMON_PROCESS) {
            holder.aotuType.setVisibility(View.INVISIBLE);
            holder.normalType.setVisibility(View.VISIBLE);
        } else if (info.type == ReceiverProcessInfo.CPU_SETTING) {
            holder.aotuType.setVisibility(View.INVISIBLE);
            holder.normalType.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        MySwitch mSwitch;
        TextView appName;
        TextView aotuType;
        TextView normalType;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        this.isChecked = isChecked;
    }

    public void setOnCheckedChanged(
            OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    public void onClick(View v) {
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged((CompoundButton) v,
                    isChecked);
        }
    }

    private void configurePinnedHeader() {
        // TODO Auto-generated method stub

    }

    @Override
    public SectionInfo getSectionInfo(int index) {
        // TODO Auto-generated method stub
        return mList.get(index);
    }

    @Override
    public void remove(int posaction) {
        if (mList != null && mList.size() > 0 && (posaction - 1) > 0) {
            mList.remove(posaction - 1);
        }
    }

    @Override
    public String getHeadTitle(int posaction) {
        return mList.get(posaction).getSection();
    }
}
